﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BS_2.Class;
using CCWin;

namespace BS_2
{
    public partial class Form_OutputData : CCWin.Skin_DevExpress
    {
        public Form_OutputData()
        {
            InitializeComponent();
        }

        private void Form_OutputData_Load(object sender, EventArgs e)
        {
            string sql = "select subname from subgeo where prjcode=" + Globale.project_code ;
            if (DataAccess.Run_SQL(sql) != 0)
            {
                skinComboBox1.Items.Clear();
                DataTable dt = DataAccess.Get_DataTable(sql,"subname");
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        skinComboBox1.Items.Add(dt.Rows[i][0].ToString());//左边下拉框添加项
                    }
                }
            }
        }

        private void skinComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql = "select subcode from subgeo where subname='" + skinComboBox1.Text + "'";
            if (DataAccess.Run_SQL(sql) != 0)
            {
                int  subcode = Convert.ToInt32(DataAccess.Get_Row1_Col1_Value(sql));
                sql = "select dataname,datacode from data where subcode=" + subcode;
                if (DataAccess.Run_SQL(sql) != 0)
                {
                    DataTable dt0 = DataAccess.Get_DataTable(sql, "1");
                    Globale.data_code = Convert.ToInt32(dt0.Rows[0][1]);
                    skinComboBox2.Items.Clear();
                    DataTable dt = DataAccess.Get_DataTable(sql, "dataname");
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            skinComboBox2.Items.Add(dt.Rows[i][0].ToString());//左边下拉框添加项
                        }
                    }
                }
            }
        }

        private void skinComboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql = "select appendname from append where datacode=" + Globale.data_code;
            if (DataAccess.Run_SQL(sql) != 0)
            {
                DataTable dt = DataAccess.Get_DataTable(sql, "prj");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    CCWin.SkinControl.SkinListBoxItem item = new CCWin.SkinControl.SkinListBoxItem(dt.Rows[i][0].ToString());
                    skinListBox1.Items.Add(item);
                }
                skinListBox1.SelectedIndex = 0;
            }
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        string path =System.IO.Path.GetDirectoryName(dialog.FileName);
                        skinTextBox1.Text = path;
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                }
            }
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            if (skinTextBox1.Text != "")
            {
                string sql = "select appenddata,appendname,appendtype from append where appendname='" + skinListBox1.SelectedItem.ToString()+"'";
                DataTable dt = DataAccess.Get_DataTable(sql, "appenddata");
                byte[] bt = (byte[])dt.Rows[0][0];
                string path;
                /*path = skinTextBox1.Text + "\\" + dt.Rows[0][1].ToString();
                BinaryWriter bw = new BinaryWriter(File.Open(path, FileMode.OpenOrCreate));
                bw.Write(bt);*/
                if (dt.Rows[0][2].ToString() == "文档")
                {
                    path = skinTextBox1.Text + "\\" + dt.Rows[0][1].ToString() + ".pdf";
                    MessageBoxEx.Show("确定要导出吗？");
                    ByteHelper.ByteFile(bt, path);
                    MessageBoxEx.Show("导出成功！");
                }
                if (dt.Rows[0][2].ToString() == "图片")
                {
                    path = skinTextBox1.Text + "\\" + dt.Rows[0][1].ToString() + ".jpg";
                    MessageBoxEx.Show("确定要导出吗？");
                    ByteHelper.ByteFile(bt, path);
                    MessageBoxEx.Show("导出成功！");

                }
                if (dt.Rows[0][2].ToString() == "矢量数据")
                {
                    path = skinTextBox1.Text + "\\" + dt.Rows[0][1].ToString() + ".zip";
                    MessageBoxEx.Show("确定要导出吗？");
                    ByteHelper.ByteFile(bt, path);
                    MessageBoxEx.Show("导出成功！");
                }
                if (dt.Rows[0][2].ToString() == "三维数据")
                {
                    path = skinTextBox1.Text + "\\" + dt.Rows[0][1].ToString() + ".csv";
                    MessageBoxEx.Show("确定要导出吗？");
                    ByteHelper.ByteFile(bt, path);
                    MessageBoxEx.Show("导出成功！");
                }
                if (dt.Rows[0][2].ToString() == "剖面图")
                {
                    path = skinTextBox1.Text + "\\" + dt.Rows[0][1].ToString() + ".zip";
                    MessageBoxEx.Show("确定要导出吗？");
                    ByteHelper.ByteFile(bt, path);
                    MessageBoxEx.Show("导出成功！");
                }
                if (dt.Rows[0][2].ToString() == "栅格数据")
                {
                    path = skinTextBox1.Text + "\\" + dt.Rows[0][1].ToString() + ".tif";
                    MessageBoxEx.Show("确定要导出吗？");
                    ByteHelper.ByteFile(bt, path);
                    MessageBoxEx.Show("导出成功！");
                }

            }
            else
            {
                MessageBoxEx.Show("请选择路径！");
            }
        }
    }
}
